package com.chanyachting.emall.action.newsactivity;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.chanyachting.emall.domain.Admin;
import com.chanyachting.emall.domain.Member;
import com.chanyachting.emall.domain.Memberorderservices;
import com.chanyachting.emall.domain.Newsactivity;
import com.chanyachting.emall.domain.ServiceOptionType;
import com.chanyachting.emall.domain.Shortmessage;
import com.chanyachting.emall.service.MemberService;
import com.chanyachting.emall.serviceImpl.MemberorderservicesBO;
import com.chanyachting.emall.util.DateUtil;
import com.chanyachting.emall.util.RequestUtil;

public class NewsactivityAction extends DispatchAction {

	
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		RequestUtil ru=new RequestUtil(request);
		
		ru.setAttribute("newsactivityList", Newsactivity.list());
		
		return mapping.findForward("list");
	}
	
	//查看所有已审核新闻活动列表(“推荐”新闻活动列表)
	public ActionForward checkPublislist(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		
		RequestUtil ru=new RequestUtil(request);
		
		ru.setAttribute("newsactivityList", Newsactivity.listByRecommend(1));
		
		return mapping.findForward("list");
	}
	
	
	//会员推荐新闻活动
	public ActionForward toCheck(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		RequestUtil ru=new RequestUtil(request);
		
		int id=Integer.parseInt(ru.getParameter("id", "0"));
		
		Newsactivity na=new Newsactivity(id);
		
		if(na!=null){
			
			//检查用户是否有对应功能权限
			Member member=(Member)ru.getSessionObject(MemberService.MEMBER_IN_SESSION, new Member());
			
			String optionType=ServiceOptionType.News_Recommended;
			
			boolean temp=MemberorderservicesBO.checkMmemberServiceByMemberAndOPType(member, optionType);
			
			response.setContentType("text/html;charset=UTF-8"); 
			
			if(temp){
				
				na.setAudit(1);  //设置待审核属性为 true
				
				if(na.update()){
					
					response.getWriter().write("提交成功，请等待审核结果。");
				}
				
			}else{
				
				response.getWriter().write("推荐失败！请购买/开启新闻活动推荐服务。");
			}
			
		}
		
		return mapping.findForward("");
	}
	
	
	//查看所有待审核新闻活动列表
	public ActionForward checklist(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		
		RequestUtil ru=new RequestUtil(request);
		
		ru.setAttribute("newsactivityList", Newsactivity.listByAudit(1));
		
		return mapping.findForward("list");
	}
	
	//发布  （审核新闻活动“推荐”成功）
	public ActionForward publish(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		RequestUtil ru=new RequestUtil(request);
		
		int id=Integer.parseInt(ru.getParameter("id", "0"));
		
		Newsactivity na=new Newsactivity(id);
		
		if(na!=null){
			
			//设置推荐属性为 true
			na.setRecommend(1);
			
			//设置待审核属性为 false
			na.setAudit(0);
			
			//设置推荐到期时间
			
			String optionType=ServiceOptionType.News_Recommended;
			
			List<Memberorderservices> list=Memberorderservices.listByOptionTypeAndActivityAndMemberId(na.getMemberId(), optionType);
			
			Memberorderservices mos=list.get(0); //获得会员新闻活动推荐服务到期时限
			
			na.setMaturityTime(mos.getLostDate());
			
			if(na.update()) ru.setSuccessMsg();
		}
		
		ru.setAttribute("newsactivity", new Newsactivity(id));
		
		//发送站内短信
		Shortmessage smsg=new Shortmessage();
		
		Admin ad=(Admin)ru.getSessionObject("admin",new Admin());
		
		smsg.setSenderId(ad.getId());
		smsg.setSender("审核管理员");
		smsg.setReaderId(na.getMemberId());
		smsg.setTitle("新闻活动发布成功");
		smsg.setContext("恭喜，新闻活动发布成功！");
		smsg.setSendDate(DateUtil.getDate());
		smsg.setStatus("00");

		smsg.send();
		
		return mapping.findForward("show");
	}
	
	//退回
	public ActionForward goback(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		RequestUtil ru=new RequestUtil(request);
		
		int id=Integer.parseInt(ru.getParameter("id", "0"));
		
		Newsactivity na=new Newsactivity(id);
		
		if(na!=null){
			
			//设置待审核属性为 false
			na.setAudit(0);
			
			//设置推荐属性为 false
			na.setRecommend(0);
			
			//设置推荐到期时间为null
			na.setMaturityTime(null);
			
			if(na.update()) ru.setSuccessMsg();
		}
		
		ru.setAttribute("newsactivity", new Newsactivity(id));
		
		//发送站内短信
		Shortmessage smsg=new Shortmessage();
		
		Admin ad=(Admin)ru.getSessionObject("admin",new Admin());
		
		smsg.setSenderId(ad.getId());
		
		smsg.setSender("审核管理员");
		smsg.setReaderId(na.getMemberId());
		smsg.setTitle("新闻活动退回");
		smsg.setContext("抱歉，您发布的新闻活动未能通过审核，已退回！");
		smsg.setSendDate(DateUtil.getDate());
		smsg.setStatus("00");

		smsg.send();
		
		
		return mapping.findForward("show");
	}
	
	
	public ActionForward show(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		RequestUtil ru=new RequestUtil(request);
		
		int id=Integer.parseInt(ru.getParameter("id", "0"));
		
		Newsactivity na=new Newsactivity(id);
		
		ru.setAttribute("newsactivity", na);
		
		return mapping.findForward("show");
	}
	
	
	

}
